package com.hedl.practice.diyJava.multithreading.thread_pool.thread_pool_test;

import com.hedl.practice.diyJava.multithreading.thread_pool.thread_pool_test.utils.Task;

import java.util.concurrent.*;

public class StudyThreadPool {

    public static void main(String[] args) {
        //创建一个固定大小的线程池
        /*ExecutorService es = Executors.newFixedThreadPool(4);
        for (int i = 0; i < 6; i++){
            es.submit(new Task(" "+i));
        }

        //关闭线程池
        es.shutdown();*/

        /**
         * 如果我们把线程池改为CachedThreadPool，由于这个线程池的实现会根据任务数量动态调整线程池的大小，所以6个任务可一次性全部同时执行。
         * 如果我们想把线程池的大小限制在4～10个之间动态调整怎么办？
         *
         * 我们查看Executors.newCachedThreadPool()方法的源码：
         *      public static ExecutorService newCachedThreadPool() {
         *          return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
         *                                          60L, TimeUnit.SECONDS,
         *                                          new SynchronousQueue<Runnable>());
         */
        int min = 4;
        int max = 10;
        ExecutorService es1 = new ThreadPoolExecutor(min,max,60L, TimeUnit.SECONDS,new SynchronousQueue<Runnable>());
        for (int i = 0; i < 6; i++){
            es1.submit(new Task(" "+i));
        }
        es1.shutdown();
    }
}

